<template>
  <div class="register-container">
    <div class="register-content">
      <div class="register-left">
        <div class="left-content">
          <div class="icon-wrapper">
            <div class="icon-container">
              <el-icon><FirstAidKit /></el-icon>
            </div>
          </div>
          <div class="title-wrapper">
            <h2 class="left-title">用户注册</h2>
        </div>
          <h1>欢迎注册</h1>
          <h2>智慧医疗服务平台</h2>
          <p>创建账号，享受全方位的医疗服务体验</p>
        </div>
      </div>
      
      <div class="register-right">
        <div class="register-form-wrapper">
          <div class="form-header">
            <h3>请填写以下信息完成注册</h3>
          </div>
          
          <el-form :model="form" :rules="rules" ref="registerForm" class="register-form">
            <el-form-item prop="username">
              <el-input 
                v-model="form.username" 
                placeholder="请设置用户名"
                class="custom-input"
              >
                <template #prefix>
                  <el-icon><User /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item prop="name">
              <el-input 
                v-model="form.name" 
                placeholder="请输入您的真实姓名"
                class="custom-input"
              >
                <template #prefix>
                  <el-icon><Avatar /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item prop="email">
              <el-input 
                v-model="form.email" 
                placeholder="请输入您的邮箱"
                class="custom-input"
              >
                <template #prefix>
                  <el-icon><Message /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item prop="phone">
              <el-input 
                v-model="form.phone" 
                placeholder="请输入您的手机号"
                class="custom-input"
              >
                <template #prefix>
                  <el-icon><Phone /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input 
                v-model="form.password" 
                type="password" 
                placeholder="请设置密码"
                show-password
                class="custom-input"
              >
                <template #prefix>
                  <el-icon><Lock /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item prop="confirmPassword">
              <el-input 
                v-model="form.confirmPassword" 
                type="password" 
                placeholder="请再次输入密码"
                show-password
                class="custom-input"
              >
                <template #prefix>
                  <el-icon><Lock /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item>
              <el-button 
                type="primary" 
                class="register-button" 
                :loading="loading"
                @click="handleRegister"
              >
                注册
              </el-button>
            </el-form-item>
            
            <div class="form-footer">
              <span>还没有账号？</span>
              <el-link type="primary" @click="toLogin" class="login-link">立即登录</el-link>
              <div class="back-home">
                <el-link type="info" @click="backToHome">
                  <el-icon><ArrowLeft /></el-icon> 返回首页
                </el-link>
              </div>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { authAPI } from '@/utils/api'
import { useUserStore } from '@/stores/user'
import { ArrowLeft, FirstAidKit, User, Avatar, Message, Phone, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()
const registerForm = ref(null)
const loading = ref(false)

// 初始化表单数据
const form = reactive({
  username: '',
  name: '',
  email: '',
  phone: '',
  password: '',
  confirmPassword: '',
  role: 'patient' // 默认注册为患者角色
})

// 验证密码匹配
const validateConfirmPassword = (rule, value, callback) => {
  if (value !== form.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

// 验证手机号码格式
const validatePhone = (rule, value, callback) => {
  const phoneRegex = /^1[3-9]\d{9}$/
  if (!phoneRegex.test(value)) {
    callback(new Error('请输入正确的手机号码'))
  } else {
    callback()
  }
}

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3-20个字符之间', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入您的姓名', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { validator: validatePhone, trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
}

// 处理注册
const handleRegister = () => {
  registerForm.value.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true
        
        // 准备注册数据
        const registerData = {
          username: form.username,
          password: form.password,
          name: form.name,
          email: form.email,
          phone: form.phone,
          role: form.role,
          // 为患者角色增加个人信息
          personalInfo: {
            gender: '未知' // 默认性别设为未知，用户可以在个人中心完善
          }
        }
        
        // 调用注册API
        await authAPI.register(registerData)
        
        ElMessage.success('注册成功，请登录')
        router.push('/login')
      } catch (error) {
        ElMessage.error(error.response?.data?.message || error.message || '注册失败')
      } finally {
        loading.value = false
      }
    }
  })
}

// 跳转到登录页
const toLogin = () => {
  router.push('/login')
}

// 返回首页
const backToHome = () => {
  router.push('/')
}
</script>

<style scoped>
.register-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  width: 100%;
  background-color: #e6f7ff;
}

.register-content {
  display: flex;
  width: 900px;
  height: 600px;
  border-radius: 0;
  overflow: hidden;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.1);
}

.register-left {
  position: relative;
  width: 40%;
  background: linear-gradient(to bottom, #1989fa, #0066cc);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 30px 20px;
}

.left-content {
  position: relative;
  z-index: 1;
  text-align: center;
  padding: 20px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.icon-wrapper {
  margin-bottom: 15px;
}

.icon-container {
  width: 70px;
  height: 70px;
  background-color: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 5px;
}

.icon-container .el-icon {
  font-size: 40px;
  color: white;
}

.title-wrapper {
  margin-bottom: 15px;
  width: 100%;
}

.left-title {
  font-size: 26px;
  font-weight: 600;
  margin-bottom: 25px;
  border-bottom: 2px solid rgba(255, 255, 255, 0.3);
  padding-bottom: 12px;
}

.left-content h1 {
  font-size: 32px;
  font-weight: 600;
  margin-bottom: 8px;
}

.left-content h2 {
  font-size: 22px;
  font-weight: 500;
  margin-bottom: 20px;
}

.left-content p {
  font-size: 16px;
  opacity: 0.9;
  max-width: 90%;
  margin: 0 auto;
  line-height: 1.6;
}

.register-right {
  width: 60%;
  background-color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.register-form-wrapper {
  width: 100%;
  max-width: 450px;
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
}

.form-header h3 {
  font-size: 18px;
  color: #333;
  font-weight: 500;
}

.register-form {
  margin-top: 10px;
}

.register-form :deep(.el-form-item) {
  margin-bottom: 22px;
}

.custom-input {
  height: 40px;
}

.register-form :deep(.el-input__wrapper) {
  padding: 0 15px;
  border-radius: 4px;
}

.register-form :deep(.el-input__prefix) {
  margin-right: 10px;
}

.register-form :deep(.el-input__inner) {
  height: 40px;
}

.register-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
  margin-top: 10px;
  border-radius: 4px;
}

.form-footer {
  text-align: center;
  font-size: 14px;
  color: #666;
  margin-top: 25px;
}

.login-link {
  margin-left: 5px;
  font-weight: 500;
}

.back-home {
  margin-top: 15px;
  text-align: center;
}

@media (max-width: 992px) {
  .register-content {
    width: 90%;
    flex-direction: column;
    height: auto;
  }
  
  .register-left,
  .register-right {
    width: 100%;
  }
  
  .register-left {
    padding: 40px 0;
  }
}

@media (max-width: 480px) {
  .register-container {
    padding: 0;
  }
  
  .register-content {
    width: 100%;
    border-radius: 0;
    box-shadow: none;
  }
  
  .register-left {
    display: none;
  }
  
  .register-right {
    padding: 30px 20px;
  }
}
</style> 